<?php
/**
 * Created by PhpStorm.
 * User: keyth
 * Date: 18-4-8
 * Time: 下午6:21
 * Des:主模块
 */

namespace app\index\controller;

use app\model\Lab;
use app\model\Stu_tea_lab;
use app\model\Student;
use app\model\Tea_lab;
use app\model\Teacher;
use app\model\Admin;
use think\Controller;
use think\Request;

class AdminController extends Controller
{
    /**
     * 析构函数
     * 确保进入后台的每个操作都是登录者才有权限进行的
     **/
    public function _initialize()
    {
        $UR_Logstate = cookie('UR_Logstate');
        if (empty(cookie('UR_distinctid')) || empty($UR_Logstate)) {
            LoginController::logout();
            $this->success('未登录,请登陆!！', url('/index/login'));
        }
        $Con = self::who();
        if ($Con->where('cookie_state', $UR_Logstate)->find()) {
        } else {
            LoginController::logout();
            $this->error('登录失效，请重新登陆后再尝试！', url('/index/login'));
        }
    }

    /**
     * 前置操作
     * 通过调整方法的前置操作进行权限控制
     **/
    protected $beforeActionList = [
        'teacher_top' => ['only' => 'tea_lab,tea_lab_c,my_tea_lab_edit,my_tea_lab,my_tea_lab_c,GetAjax_select_lab'],
        'student_top' => ['only' => 'stu_tea_lab_list,my_stu_tea_lab_list,my_stu_tea_lab_c,stu_tea_lab_c'],
        'admin_top' => ['only' => 'student,studentadd,studentadd_c,studentdel,stu_password,teacher,teacheradd,teacheradd_c,teacherdel,tea_password,lab,labadd,labadd_c,labdel']
    ];

    /**
     * 前置方法
     * 进行权限的控制
     *
     **/
    public function teacher_top()
    {
        $UR_distinctid = base64_decode(base64_decode(cookie('UR_distinctid')));
        if ($UR_distinctid == 'Admin') {
            ;
        } elseif ($UR_distinctid == 'Teacher') {
            ;
        } else {
            $this->error('没有权限', url('/index/admin/welcome'));
        }
    }

    public function student_top()
    {
        $UR_distinctid = base64_decode(base64_decode(cookie('UR_distinctid')));
        if ($UR_distinctid == 'Admin') {
            ;
        } elseif ($UR_distinctid == 'Student') {
            ;
        } else {
            $this->error('没有权限', url('/index/admin/welcome'));
        }
    }

    public function admin_top()
    {
        $UR_distinctid = base64_decode(base64_decode(cookie('UR_distinctid')));
        if ($UR_distinctid == 'Admin') {
            ;
        } else {
            $this->error('没有权限', url('/index/admin/welcome'));
        }
    }

    /**
     * 后台主页 and welcome
     **/
    public function index()
    {
        $user = self::GetInfo();
        $this->assign('user', $user);
        return $this->fetch();
    }

    public function welcome()
    {
        $Student = NEW Student ();
        $Teacher = NEW Teacher();
        $Tea_lab = NEW Tea_lab();
        $Lab = NEW Lab();
        $Stu_tea_lab = NEW Stu_tea_lab();
        $numbers = array();
        $numbers['stu'] = $Student->count();
        $numbers['tea'] = $Teacher->count();
        $numbers['tea_lab'] = $Tea_lab->count();
        $numbers['lab'] = $Lab->count();
        $numbers['stu_tea'] = $Stu_tea_lab->count();
        $this->assign('numbers', $numbers);
        return $this->fetch();
    }

    /**
     * @return mixed
     * 学生管理模块
     */
    public function student()
    {

        $Student = NEW Student();
        $gets = Request::instance()->get();
        if (!empty($gets)) {
            if (!is_null($gets['username'])) {
                $Student->where('username', 'like', '%' . $gets["username"] . '%');
            }
        }
        $Students = $Student->paginate();
        $this->assign('students', $Students);
        return $this->fetch('stu_list');
    }

    public function studentadd()
    {

        $id = Request::instance()->get('id/d');
        if (!empty($id)) {
            $Student = Student::get($id);
        } else {
            $Student = NEW Student();
            $Student->id = '';
            $Student->address = '';
            $Student->sex = '';
            $Student->age = '';
            $Student->email = '';
            $Student->mobile = '';
            $Student->username = '';
            $Student->password = '';
        }
        $this->assign('student', $Student);
        return $this->fetch('stu_add');
    }

    public function studentadd_c()
    {
        $postdata = Request::instance()->post();
        $state = Student::Stu_reg($postdata);
        if ($state == 1) {
            return alertjsclose('学生信息添加/修改成功!');
        }
        return alertjsclose('学生信息添加/修改失败,请重新尝试!');
    }

    public function studentdel()
    {
        $id = Request::instance()->get('id/d');
        if (!empty($id)) {
            if (Student::get($id)->delete()) {
                $Stu_tea_lab = NEW Stu_tea_lab ();
                $group_tls = $Stu_tea_lab->where('stu_id', $id)->group('tea_lab_id')->field('tea_lab_id')->select();
                foreach ($group_tls as $item) {
                    $number = $Stu_tea_lab->where('tea_lab_id', $item->tea_lab_id)->where('stu_id', $id)->delete();
                    $tea_lab = Tea_lab::get($item->tea_lab_id);
                    $tea_lab->number = $tea_lab->number - $number;
                    $tea_lab->save();
                }
            }
            return 1;
        }
        return 0;
    }

    public function stu_password()
    {

        $newpass = Request::instance()->get('password/d');
        $id = Request::instance()->get('id/d');
        if (!is_null($newpass)) {
            $teacher = Student::get($id);
            $teacher->password = encrypt_pass($teacher->getData('email'), $newpass);
            $teacher->save();
            return alertjsclose('更新密码成功!');
        }

        return $this->fetch('password');
    }

    /**
     * @return mixed
     * 教师管理模块
     */

    public function teacher()
    {
        $Teacher = NEW Teacher();
        $gets = Request::instance()->get();
        if (!empty($gets)) {
            if (!is_null($gets['username'])) {
                $Teacher->where('username', 'like', '%' . $gets["username"] . '%');
            }
        }
        $teachers = $Teacher->paginate();
        $this->assign('teachers', $teachers);
        return $this->fetch('tea_list');
    }

    public function teacheradd()
    {

        $id = Request::instance()->get('id/d');
        if (!empty($id)) {
            $Teacher = Teacher::get($id);
        } else {
            $Teacher = NEW Teacher();
            $Teacher->id = '';
            $Teacher->address = '';
            $Teacher->sex = '';
            $Teacher->age = '';
            $Teacher->email = '';
            $Teacher->mobile = '';
            $Teacher->username = '';
            $Teacher->password = '';
        }

        $this->assign('teacher', $Teacher);
        return $this->fetch('tea_add');
    }

    public function teacheradd_c()
    {
        $postdata = Request::instance()->post();
        $state = Teacher::Tea_reg($postdata);
        if ($state == 1) {
            return alertjsclose('教师信息添加/修改成功!');
        }
        return alertjsclose('教师信息添加/修改失败,请重新尝试!');
    }

    public function teacherdel()
    {
        $id = Request::instance()->get('id/d');
        if (!empty($id)) {
            $Tea_lab = NEW Tea_lab ();
            $tea_labs = $Tea_lab->where('tea_id', $id)->select();
            $sum = 0;
            foreach ($tea_labs as $item) {
                $sum += $item->number;
            }
            if ($sum == 0) {
                $Tea_lab->where('tea_id', $id)->delete();
            } else {
                return -1;
            }
            Teacher::get($id)->delete();
            return 1;
        }
        return 0;
    }

    public function tea_password()
    {

        $newpass = Request::instance()->get('password/d');
        $id = Request::instance()->get('id/d');
        if (!is_null($newpass)) {
            $teacher = Teacher::get($id);
            $teacher->password = encrypt_pass($teacher->getData('email'), $newpass);
            $teacher->save();
            return alertjsclose('更新密码成功!');
        }

        return $this->fetch('password');
    }

    /**
     * @return mixed
     * 实验室管理模块
     */
    public function lab()
    {
        $week = Request::instance()->get('week/d');
        $address = Request::instance()->get('address');
        $node = Request::instance()->get('node/d');
        $Lab = new Lab();
        if (!empty($week)) {
            $Lab->where('week', $week);
        }
        if (!empty($address)) {
            $Lab->where('address', $address);
        }
        if (!empty($node)) {
            $Lab->where('node', $node);
        }
        $labs = $Lab->paginate();
        $addres_labs = $Lab->where('state', 0)->group('address')->column('address');
        $this->assign('labs', $labs);
        $this->assign('address_hover', $address);
        $this->assign('week_hover', $week);
        $this->assign('node_hover', $node);
        $this->assign('addres_labs', $addres_labs);
        $this->assign('address', $address);
        return $this->fetch('lab_list');
    }

    public function labadd()
    {
        $id = Request::instance()->get('id/d');
        if (!empty($id)) {
            $Lab = Lab::get($id);
        } else {
            $Lab = NEW Lab();
            $Lab->id = '';
            $Lab->name = '';
            $Lab->address = '';
            $Lab->week = '';
            $Lab->node = '';
        }

        $this->assign('lab', $Lab);
        return $this->fetch('lab_add');
    }

    public function labadd_c()
    {
        $postdata = Request::instance()->post();
        if (!empty($postdata['id'])) {
            $Lab = Lab::get($postdata['id']);
        } else {
            $Lab = new Lab();
        }
        if ($Lab->where('id', '<>', $postdata['id'])->where('address', $postdata['address'])->where('week', $postdata['week'])->where('node', $postdata['node'])->select()) {
            return alertjsclose('失败!该实验室在本时段已经被添加过一次了!');
        }
        if (!empty($postdata['id'])) {
            $Lab = Lab::get($postdata['id']);
        }
        $Lab->address = $postdata['address'];
        $Lab->address = $postdata['address'];
        $Lab->week = $postdata['week'];
        $Lab->name = $postdata['name'];
        $Lab->node = $postdata['node'];
        $Lab->state = 0;
        //$Lab->admin_id=;

        $state = $Lab->save();
        if ($state)
            return alertjsclose('实验室信息添加/修改成功!');
        return alertjsclose('实验室信息添加/修改失败,请重新尝试!');
    }

    public function labdel()
    {
        $id = Request::instance()->get('id/d');
        if (!empty($id)) {

            $lab = Lab::get($id);
            if ($lab->state == 1) {
                return -1;
            } else {
                $lab->delete();
                return 1;
            }
        }
        return 0;
    }

    /**
     *  @教师and实验室模块
     **/
    public function tea_lab()
    {
        $week = Request::instance()->get('week/d');
        $address = Request::instance()->get('address');
        $node = Request::instance()->get('node/d');
        $Lab = new Lab();
        if (!empty($week)) {
            $Lab->where('week', $week);
        }
        if (!empty($address)) {
            $Lab->where('address', $address);
        }
        if (!empty($node)) {
            $Lab->where('node', $node);
        }
        $labs = $Lab->where('state', 0)->paginate(10);
        $addres_labs = $Lab->where('state', 0)->group('address')->column('address');
        $this->assign('node_hover', $node);
        $this->assign('address_hover', $address);
        $this->assign('week_hover', $week);
        $this->assign('addres_labs', $addres_labs);
        $this->assign('labs', $labs);
        return $this->fetch('tea_lab_list');
    }

    /**
     * state: 1-正常上课   0-休息
     **/
    public function tea_lab_c()
    {
        $max_number = Request::instance()->get('max_number/d');
        $lab_id = Request::instance()->get('lab_id/d');
        $name = Request::instance()->get('name');
        $Teacher = self::who();
        $tea_id = $Teacher->where('cookie_state', cookie('UR_Logstate'))->column('id');
        $Tea_lab = new Tea_lab();
        $Tea_lab->tea_id = $tea_id[0];
        $Tea_lab->lab_id = $lab_id;
        $Tea_lab->max_number = $max_number;
        $Tea_lab->name = $name;
        $Tea_lab->number = 0;
        $Tea_lab->state = 1;
        if ($Tea_lab->save()) {
            $lab = Lab::get($lab_id);
            $lab->state = 1;
            $lab->save();
            return 1;
        }
        return 0;
    }

    /**
     *  教师 个人and实验室模块
     **/
    public function my_tea_lab()
    {

        $Tea_lab = new Tea_lab();
        $User = self::GetInfo();
        $tea_labs = $Tea_lab->where('tea_id', $User->id)->paginate(10);
        $this->assign('tea_labs', $tea_labs);
        return $this->fetch('my_tea_lab_list');
    }

    public function my_tea_lab_edit()
    {
        $id = Request::instance()->get('id/d');
        $Lab = new Lab();

        $addres_labs = $Lab->where('state', 0)->group('address')->column('address');
        $week_labs = $Lab->where('state', 0)->group('week')->column('week');
        $node_labs = $Lab->where('state', 0)->group('node')->column('node');
        $this->assign('node_labs', $node_labs);
        $this->assign('addres_labs', $addres_labs);
        $this->assign('week_labs', $week_labs);
        $this->assign("tea_lab", Tea_lab::get($id));
        return $this->fetch();
    }

    public function my_tea_lab_c()
    {
        $max_number = Request::instance()->get('max_number/d');
        $lab_id = Request::instance()->get('labid/d');
        $name = Request::instance()->get('name');
        $currentid = Request::instance()->get('id/d');
        $newid = Request::instance()->get('new_labid/d');
        $tea_lab = Tea_lab::get($currentid);
        if (!empty($newid)) {
            $lab = Lab::get($tea_lab->lab_id);
            $lab->state = 0;
            $lab->save();
            $tea_lab->lab_id = $newid;
            $lab = Lab::get($newid);
            $lab->state = 1;
            $lab->save();
        }
        $tea_lab->max_number = $max_number;
        $tea_lab->name = $name;
        $tea_lab->save();
        return 1;
    }

    /**
     * stu_tea_lab_list
     * retuen view
     * @学生选择教师课程----学生--教师--实验室
     **/
    public function stu_tea_lab_list()
    {
        $tea_id = Request::instance()->get('tea_id/d');
        $lab_id = Request::instance()->get('lab_id/d');
        $Tea_lab = new Tea_lab();
        $user = self::GetInfo();
        if (!empty($tea_id)) {
            $Tea_lab->where('tea_id', $tea_id);
        }
        if (!empty($lab_id)) {
            $Tea_lab->where('lab_id', $lab_id);
        }
        $tea_labs = $Tea_lab->paginate();
        $teas = $Tea_lab->group('tea_id')->field('tea_id')->select();
        $labs = $Tea_lab->group('lab_id')->field('lab_id')->select();
        $this->assign('user_id', $user->id);
        $this->assign('tea_hover', $tea_id);
        $this->assign('lab_hover', $lab_id);
        $this->assign('teas', $teas);
        $this->assign('labs', $labs);
        $this->assign('tea_labs', $tea_labs);
        return $this->fetch();
    }

    public function my_stu_tea_lab_list()
    {
        $Stu_tea_lab = new Stu_tea_lab();
        $user = self::GetInfo();
        $tea_labs = $Stu_tea_lab->where('stu_id', $user->id)->paginate();
        $this->assign('tea_labs', $tea_labs);
        return $this->fetch();
    }

    /**
     * 学生取消预约
     **/
    public function my_stu_tea_lab_c()
    {
        $id = Request::instance()->get('id');
        if (!empty($id)) {
            $stu_tea_lab = Stu_tea_lab::get($id);
            $tea_lab = Tea_lab::get($stu_tea_lab->tea_lab_id);
            if ($stu_tea_lab->delete()) {
                $tea_lab->number = $tea_lab->number - 1;
                $tea_lab->save();
            } else {
                return 0;
            }
            return 1;
        }
        return 0;
    }

    /**
     * 学生预约
     **/
    public function stu_tea_lab_c()
    {
        $user = self::GetInfo();
        $stu_id = $user->id;
        $tea_lab_id = Request::instance()->get('tea_lab_id/d');
        $tea_lab = Tea_lab::get($tea_lab_id);
        if ($tea_lab->max_number > $tea_lab->number) {
            $Stu_tea_lab = new Stu_tea_lab();
            $Stu_tea_lab->stu_id = $stu_id;
            $Stu_tea_lab->tea_lab_id = $tea_lab_id;
            $Stu_tea_lab->state = 1;
            if ($Stu_tea_lab->save()) {
                $tea_lab->number = $tea_lab->number + 1;
            }
            $tea_lab->save();
        }
        return 1;
    }

    /**
     * who
     * retuen object
     * des:确定当前登录者的身份并返回相应的模型
     **/
    private static function who()
    {
        $UR_distinctid = base64_decode(base64_decode(cookie('UR_distinctid')));
        if ($UR_distinctid == 'Admin') {
            $Con = new Admin();
        } elseif ($UR_distinctid == 'Teacher') {
            $Con = new Teacher();
        } else {
            $Con = new Student();
        }
        return $Con;
    }

    /**
     * GetInfo
     * retuen object
     * des:返回当前登录者信息的对象
     **/
    private static function GetInfo()
    {
        $Con = self::who();
        $users = $Con->where('cookie_state', cookie('UR_Logstate'))->select();
        return $users[0];
    }

    /**
     * GetAjax_select_lab
     * return -1--失败   array--成功并返回数据
     **/
    public function GetAjax_select_lab()
    {
        $week = Request::instance()->get('week/d');
        $node = Request::instance()->get('node/d');
        $Lab = new Lab ();
        if (!empty($node)) {
            $Lab->where('node', $node);
        }
        if (!empty($week)) {
            $Lab->where('week', $week);
        }
        $Lab_address = $Lab->where('state', 0)->field('id,address')->select();
        if ($Lab_address) {
            return json_encode($Lab_address);
        }
        return -1;

    }

    /**
     * 个人信息修改模块
     **/
    public function user_info()
    {
        $Con = self::GetInfo();
        $this->assign('student', $Con);
        return $this->fetch();
    }

    public function user_info_c()
    {

        $Con = self::GetInfo();
        $postdata = Request::instance()->post();
        $Con->address = $postdata['address'];
        $Con->sex = $postdata['sex'];
        $Con->age = $postdata['age'];
        $Con->mobile = $postdata['mobile'];
        $Con->username = $postdata['username'];
        if ($Con->save()) {
            return alertjsclose('个人信息修改成功!');
        }
        return alertjsclose('系统繁忙!个人信息修改失败!');
    }

    /**
     * Admin模块
     **/

    public function admintll()
    {

        $Tea_lab = new Tea_lab();
        $tea_labs = $Tea_lab->paginate(10);
        $this->assign('tea_labs', $tea_labs);
        return $this->fetch('admin_tea_lab_list');
    }

    public function admintle()
    {
        $id = Request::instance()->get('id/d');
        $Lab = new Lab();
        $addres_labs = $Lab->where('state', 0)->group('address')->column('address');
        $week_labs = $Lab->where('state', 0)->group('week')->column('week');
        $node_labs = $Lab->where('state', 0)->group('node')->column('node');
        if (!empty($id)) {
            $tea_lab = Tea_lab::get($id);
        } else {
            $tea_lab = new Tea_lab ();
            $tea_lab->name = '';
            $tea_lab->max_number = '';
            $tea_lab->lab_id = '';
            $tea_lab->tea_id = '';
            $tea_lab->id = '';

        }

        $teachers = Teacher::all();
        $this->assign('teachers', $teachers);
        $this->assign('node_labs', $node_labs);
        $this->assign('addres_labs', $addres_labs);
        $this->assign('week_labs', $week_labs);
        $this->assign("tea_lab", $tea_lab);
        return $this->fetch('admin_tea_lab_edit');
    }

    public function admintle_c()
    {
        $max_number = Request::instance()->get('max_number/d');
        $lab_id = Request::instance()->get('labid/d');
        $name = Request::instance()->get('name');
        $currentid = Request::instance()->get('id/d');
        $newid = Request::instance()->get('new_labid/d');
        $teacher_id = Request::instance()->get('teacher_id/d');
        $tea_lab = Tea_lab::get($currentid);
        if (!empty($newid)) {
            $lab = Lab::get($tea_lab->lab_id);
            $lab->state = 0;
            $lab->save();
            $tea_lab->lab_id = $newid;
            $tea_lab->tea_id = $teacher_id;
            $lab = Lab::get($newid);
            $lab->state = 1;
            $lab->save();
        }
        $tea_lab->max_number = $max_number;
        $tea_lab->name = $name;
        $tea_lab->save();
        return 1;
    }

    public function adminstll()
    {
        $Stu_tea_lab = new Stu_tea_lab();
        $tea_labs = $Stu_tea_lab->paginate();
        $this->assign('tea_labs', $tea_labs);
        return $this->fetch('admin_stu_tea_lab_list');
    }

    public function adminpw()
    {
        $newpass = Request::instance()->get('password');
        $id = Request::instance()->get('id/d');
        if (!is_null($newpass)) {
            $Con =self::who();
            $teacher=$Con::get($id);
            $teacher->password = encrypt_pass($teacher->getData('email'), $newpass);
            $teacher->save();
            return alertjsclose('更新密码成功!');
        }
        return $this->fetch('password');
    }



}